<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-10-20
 * Time: 下午16:36
 */
 if(!isset($_SESSION['sid']))exit('{"status":"fail","msg":"登陆过期","code":0}');
 if(!isset($_REQUEST['action'])) exit('{"status":"fail","msg":"参数错误"}');
use \PFT\Db;
include_once '/var/www/html/new/d/module/common/Db.class.php';
include_once '/var/www/html/new/d/common/func.inc.php';
$dbConf = include_once '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
Db::Conf($dbConf['remote_1']);
$op = (isset($_REQUEST['bt'])) ? safetxt($_REQUEST['bt']):date('Y-m-d',strtotime('-1 month'));// 30 day
$ed = (isset($_REQUEST['et'])) ? safetxt($_REQUEST['et']):date('Y-m-d');
$pageSize    = (isset($_REQUEST['pageSize'])) ? $_REQUEST['pageSize']+0:14;
$currentPage = (isset($_REQUEST['currentPage'])) ? $_REQUEST['currentPage']+0:1;
$pStart      = ($currentPage-1) * $pageSize.',';
/*输入标题*/
$t=safetxt(trim($_REQUEST['title']));
$title       = (isset($_REQUEST['title']))?"and title like '%{$t}%'":'';
switch(safetxt(trim($_REQUEST['action']))){
    //展示公告内容
    case '1':
        $where = "where status=0 and type=0 and create_time between '$op 00:00:00' and '$ed 23:59:59' $title";
        $result= announce($where,$pageSize,$pStart);
        $totalPage  = $result['total'];
        unset($result['total']);
        echo json_encode(array('currentPage'=>$currentPage,'status'=>'success','pageSize'=>$pageSize,
                                'totalPage'=>$totalPage,'list'=>$result));
        break;

    //发布公告的草稿箱
    case '2':
        $where  = "where status=1 and type=0 and create_time between '$op 00:00:00' and '$ed 23:59:59' $title";
        $result = announce($where,$pageSize,$pStart);
        $totalPage  = $result['total'];
        unset($result['total']);
        echo json_encode(array('currentPage'=>$currentPage,'status'=>'success','pageSize'=>$pageSize,
                                'totalPage'=>$totalPage,'list'=>$result));
        break;

    //用户查看发布公告
    case '3':
        $where  = "where status=0 and type=0  $title";
        $result = announce($where,$pageSize,$pStart);
        $totalPage= $result['total'];
        unset($result['total']);
        echo json_encode(array('currentPage'=>$currentPage,'status'=>'success','pageSize'=>$pageSize,
                                'totalPage'=>$totalPage,'list'=>$result));
        break;
    //展示在登陆后的首要
    case '4':
        $where = "where status=0 and type=0 ";
        $pageSize=10;
        $pStart='';
        $result= announce($where,$pageSize,$pStart);
        foreach($result as $key=>$val){
            unset($result[$key]['creater'],$result[$key]['details']);
            $result[$key]['create_time']=date('m-d',strtotime($result[$key]['create_time']));
        }
        echo json_encode(array('status'=>'success','list'=>$result));
        break;
    default:
        echo json_encode(array('status'=>'fail','msg'=>'参数错误'));
        break;
}
    function announce($where,$pageSize,$pStart){
        $sql = "select id,title,create_time,creater,details from pft_announce ".$where.
               " order by create_time desc limit $pStart$pageSize";
        $stmt =Db::Connect()->prepare($sql);
        $stmt->execute();
        $result=$id_list=$remarks=array();
        while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
            $result[]=$row;
            $id_list[]=$row['id'];
        }
        if(empty($result)){
            Db::Close();
            exit('{"status":"fail","msg":"无相关数据"}');
        }
        if(safetxt(trim($_REQUEST['action']))!=4){
            //评论数、点赞数
            $sql = "select count(id) as remarks,an_id from pft_announce_remark where an_id in ("
                .implode(',',$id_list).") group by an_id";
            $stmt      = Db::Connect()->prepare($sql);
            $stmt->execute();
            while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
                $remarks[$row['an_id']]=$row['remarks'];
            }
            // $result[$key]['remarks']=$stmt->fetchColumn();
            foreach($result as $key=>$val){
                $result[$key]['remarks'] = empty($remarks[$val['id']])?0:$remarks[$val['id']];
            }
            //计算总共几条公告
            $sql_total="select count(id) as c from pft_announce ".$where;
            $stmt      = Db::Connect()->prepare($sql_total);
            $stmt->execute();
            $total=$stmt->fetchColumn();
            $result['total']=ceil($total/$pageSize);
        }
        return $result;
    }
Db::Close();